var app = getApp();
const util = require('../../utils/util.js')
const date = require('../../utils/date.js')
Page({
  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    size: 10,
    list: [],
    isFirst: true,
    loginshow: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    text: ["@所有人，以后就来这里听豫剧吧，专业听戏用这一个就够了", "这里的豫剧很全哦！快来和我一起听戏吧！", "很全很专业的豫剧小程序，一起听戏吧！"],

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    wx.getSetting({
      success: res => {
        if (typeof(res.authSetting['scope.userInfo']) == 'undefined') {
          this.setData({
            loginshow: true
          })
        }
      }
    })
    var data = {
      page: this.data.page,
      size: this.data.size
    };
    util.http(app.globalData.getVideoList, 'POST', data, this.porcessData);
    console.log('index openid:[' + app.globalData.openid + ']');
    wx.showNavigationBarLoading();
    wx.showLoading();
  },

  porcessData: function(res) {
    console.log(res);
    var list = [];
    var totalList = [];
    for (var ids in res.data.videos) {
      var detail = res.data.videos[ids];
      var temp = {
        id: detail.id,
        title: detail.title,
        imgUrl: detail.pic,
        hits: detail.playCount,
        duration: detail.duration
      }
      list.push(temp);
    }
    if (!this.data.isFirst) {
      if (list.length < 1) {
        wx.showToast({
          title: '没有更多数据了',
          icon: 'none',
          duration: 2000
        })
      }
      totalList = this.data.list.concat(list);
    } else {
      totalList = list;
      this.data.isFirst = false;
    }
    this.setData({
      list: totalList
    })

    wx.hideNavigationBarLoading();
    wx.hideLoading();
  },
  search: function() {
    wx.navigateTo({
      url: '/pages/search/search',
    })
  },
  bindGetUserInfo: function(e) {
    if (e.detail.userInfo) {
      app.globalData.userInfo = e.detail.userInfo;
      this.saveUserInfo(e.detail.userInfo);
    }
    this.setData({
      loginshow: false
    })
  },
  saveUserInfo(user) {
    let data = {
      openid: app.globalData.openid,
      username: user.nickName,
      pic: user.avatarUrl
    }
    util.http(app.globalData.saveUser, 'POST', data, null);
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    this.setData({
      page: 1,
      list: [],
      isFirst: true
    });
    var data = {
      page: this.data.page,
      size: this.data.size
    };
    util.http(app.globalData.getVideoList, 'POST', data, this.porcessData);
    wx.stopPullDownRefresh();
    wx.showNavigationBarLoading();
    wx.showLoading();

  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    var page = this.data.page;
    page++;
    this.setData({
      page: page
    });
    var data = {
      page: this.data.page,
      size: this.data.size
    };
    util.http(app.globalData.getVideoList, 'POST', data, this.porcessData);
    wx.showNavigationBarLoading();
    wx.showLoading();
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function(res) {

    if (res.from === 'button') {
      // 来自页面内转发按钮
    }
    return {
      title: this.data.text[Math.floor(Math.random() * 3)],
      imageUrl: "https://yj-1256481299.cos.ap-guangzhou.myqcloud.com/img/index_fx.jpg",
      path: '/pages/index/index?openid=' + app.globalData.openid,
      success: function(res) {
        //console.log(res)
        wx.showToast({
          title: '发送成功',
          icon: 'none'
        })
      },
      fail: function(res) {
        // 转发失败
        // _audioplay()
        wx.showToast({
          title: '发送失败',
          icon: 'none'
        })
      }
    }
    console.log(e);
  }
})